<?php

if (!defined('BASEPATH')) exit('No direct script access allowed');

/* v3.1.0  */
	
class Home extends M_Controller {

    /**
     * 构造函数
     */
    public function __construct() {
        parent::__construct();
        $this->load->model('user_model');
    }

    /**
     * 首页
     */
    public function index() {

//        ini_set("display_errors", "on"); // 显示错误提示，仅用于测试时排查问题
//// error_reporting(E_ALL); // 显示所有错误提示，仅用于测试时排查问题
//        set_time_limit(0); // 防止脚本超时，仅用于测试使用，生产环境请按实际情况设置
//        header("Content-Type: text/plain; charset=utf-8"); // 输出为utf-8的文本格式，仅用于测试
//
//// 验证发送短信(SendSms)接口
//        print_r(sendSms('18201994564','1249',1));exit;
		// 登录验证
		$url = dr_user_url('login/index', array('backurl' => urlencode(dr_now_url())));
//		!$this->uid && $this->user_msg(fc_lang('会话超时，请重新登录').$this->user_model->logout(), $url);
        if(IS_POST){
            //判断是否是修改信息
            $uid = $this->input->get_post('uid');
            $name = $this->input->get_post('name');
            $sex = (int)$this->input->get_post('sex');
            $province = $this->input->get_post('province');
            $city  = $this->input->get_post('city');
            $area = $this->input->get_post('area');
            $img = $this->input->get_post('img');
            if(!$uid){
                $error = fc_lang('用户不存在');
            }
            $data = array("name"=>$name,"sex"=>$sex,"province"=>$province,"city"=>$city,"area"=>$area);
            if(!empty($img)){
                $data['avatar'] = $img;
            }
            $result = $this->db->where('id', $uid)->update('user', $data);
            if(!$result){
                $error = fc_lang('更新用户资料失败');
            }
            if(IS_AJAX){
                exit(json_encode(array(
                    'status' => 1,
                    'msg' => 'ok'
                )));
            }
            $this->user_msg(fc_lang('更新用户资料成功'),1,  dr_now_url(),  3);
        }
//		$this->load->library('dip');
        $user = $this->db->where('id',get_cookie('user_id'))->get('user')->row_array();
        //获取订单
        $orders = $this->db->where('mobile',$user['mobile'])->order_by('status asc')->get('course_order')->result_array();
//        print_r($order);exit;
        $list = array();
        if(count($orders)){
            foreach($orders as $order){
               $course = $this->db->where('order_id',$order['id'])->get('order_course')->result_array();
                $order['courses'] = $course;
                $list[] = $order;
            }
        }
		$this->template->assign(array(
			'indexu' => 1,
            'user'=> $user,
            'data' => json_encode($user),
            'orders'=> $list,
//			'invite' => dr_get_invite($this->uid, 'username'),
//			'invite_url' => dr_user_url('register/index', array('uid' => $this->uid, 'invite' => $this->member['username'])),

		));
		$this->template->display('../gw/mine.html');
    }
    //上传用户头像
    public function uploadImg(){
        $config['upload_path']   = './uploadfile/user/';
        $config['allowed_types']  = 'gif|jpg|png';
        $config['max_size']   =  2000;
        $config['max_width']   = 1000;
        $config['max_height']  = 1000;

        $this->load->library('upload', $config);
        if (!$this->upload->do_upload('img'))
        {
            $error = array('error' => $this->upload->display_errors());
            $result['msg'] = $error  ;
            $result['status'] = 0;
//            $this->load->view('upload_form', $error);
        }else
        {
            $data = array('upload_data' => $this->upload->data());
            $result = array();
            $result['img_url'] =   $config['upload_path'].$data['upload_data']['orig_name'];
            $result['status'] = 1;
            echo json_encode($result);exit;
//            $this->load->view('upload_success', $data);
        }
    }

    public function hx_login_user_check(){
        $mobile = $this->input->get_post('mobile');
        $name = $this->input->get_post('name');

        $where = array();
        $where['mobile'] = $mobile;
        $where['name'] = $name;
        //print_r($where);

        $result = $this->db->where($where)->order_by('id desc')->get('course_order')->row_array();
        $arr = array();
        if($result['mobile'] == $mobile){
            //echo " 手机号对应成功 ";
            //echo json_encode($arr);exit;
        }
        if($result['name'] == $name){
            //echo " 名称对应成功 ";
            //echo json_encode($arr);exit;
        }
    }


}